<?php

namespace App\Trip\Train\TrainSchedule;

use Exception;
use Illuminate\Support\Facades\Validator;

class TrainSchedule implements TrainScheduleInterface
{
    const REQUIRED_FIELDS
        = [
            "train_no"           => "",
            "train_code"         => "required",
            "start_station_code" => "required",
            "end_station_code"   => "required",
            "minutes"            => "required",
            "day_difference"     => "required",
            "start_time"         => "required",
            "end_time"           => "required",
        ];
    private string $train_no = '';
    private string $train_code;
    private string $start_station_code;
    private string $end_station_code;
    private string $start_time;
    private string $end_time;
    private string $minutes;
    private int    $day_difference;

    /**
     * @throws Exception
     */
    public function __construct(array $trainDatas)
    {
        $validator = Validator::make($trainDatas, self::REQUIRED_FIELDS);
        if ($validator->fails()) {
            throw new Exception("参数错误");
        }
        foreach ($trainDatas as $key => $data) {
            if (is_null($data)) {
                if (gettype($this->$key) == 'string') {
                    $data = '';
                } else {
                    $data = 0;
                }
            }
            $this->$key = $data;
        }
    }

    public function __get($key)
    {
        return $this->$key;
    }

    public function __set(string $name, $value): void
    {
        $this->$name = $value;
    }

    public function setArriveTime(string $arrive_time)
    {
        // TODO: Implement setArriveTime() method.
    }

    public function __toString()
    {
        return json_encode($this->toArray(), true);
    }

    public function toArray(): array
    {
        $arr = [];
        foreach (self::REQUIRED_FIELDS as $field => $unused) {
            $arr[$field] = $this->$field;
        }
        return $arr;
    }

    public function setDepartTime(string $depart_time)
    {
        // TODO: Implement setDepartTime() method.
    }

    public function setDayDiff(int $day)
    {
        // TODO: Implement setDayDiff() method.
    }

    public function getTrainCode(): string
    {
        return $this->train_code;
    }

    public function setTrainCode(string $train_code)
    {

    }

    public function getTrainNo(): string
    {
        return $this->train_no;
    }

    public function setTrainNo(string $train_no)
    {
        // TODO: Implement setTrainNo() method.
    }

    public function getStartStationCode(): string
    {
        return $this->start_station_code;
    }

    public function setStartStationCode(string $start_code)
    {
        // TODO: Implement setStartStationCode() method.
    }

    public function getEndStationCode(): string
    {
        return $this->end_station_code;
    }

    public function setEndStationCode(string $end_code)
    {
        // TODO: Implement setEndStationCode() method.
    }

    public function getDepartTime(): string
    {
        return $this->start_time;
    }

    public function getArriveTime(): string
    {
        return $this->end_time;
    }

    public function getRunTime(): int
    {
        return $this->minutes;
    }

    public function setRunTime(string $run_time)
    {

    }

    public function getDayDiff(): int
    {
        return $this->day_difference;
    }
}